package com.wan.data.mysql.orm.convert;

import com.wan.data.mysql.orm.convert.auto.JsonDefaultConverter;
import com.wan.core.strategy.StrategySelectorCreator;
import com.wan.core.strategy.selector.StrategyCustomMapSelector;

import java.util.Collection;

public class AutoConverterCreator extends StrategySelectorCreator<Class<?>,AutoConverter, StrategyCustomMapSelector<Class<?>,AutoConverter>> {

    @Override
    protected StrategyCustomMapSelector<Class<?>, AutoConverter> newSelector(Collection<AutoConverter> objs) {
        StrategyCustomMapSelector<Class<?>,AutoConverter> selector = new StrategyCustomMapSelector<>(objs,map->{
            for (AutoConverter autoConverter : objs) {
                Class<?>[] classes = autoConverter.convertType();
                if(classes == null || classes.length == 0){
                    continue;
                }
                for (Class<?> cls : classes) {
                    map.put(cls,autoConverter);
                }
            }
        });
        //设置默认序列化方式(未有指定类型)
        selector.setDefaultStrategy(new JsonDefaultConverter());
        return selector;
    }

}
